// pages/recruit/recruit.js
const app = getApp()
import { HTTP } from '../../utils/https.js'
let http = new HTTP()
var util = require('../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    content: '',
    formats: {}, // 样式
    placeholder: '开始输入...',
    signing_type: 0, //是否面议
    date: '', //出身年月
    jobarray: [],
    job: '角色', //职业值
    job_id: '', //职业id
    start_data: '开始时间', //开始时间
    end_data: '结束时间', //结束时间
    checkwhat: '', //判断是什么类型
    movieList:[],
    movie:'影片类型',
    movieid:'',
    //城市搜索开始
    procive: '',
    city: '',
    region: [],
    provinces: [],
    cityid: '',
    citys: [],
    provinceid: '',
    countys: '',
    countysid: '',
    key: '',
  //城市搜索结束
    title:'', //获取的标题
    checkurl:'checkurl',
    id:null
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that= this;
    if (!!options.edit){
      that.setData({
        checkurl: options.edit
      })
    }
    this.setData({
      checkwhat: options.check,
      id: options.demand_id
    });
    
    var url = ""; //判断是什么url
    if(options.check == 'man'){
      url = 'mini/v1/release/job'
    }else{
      url = 'mini/v1/release/lease'
    }
    var that = this;
    var data = {
      group_by: ['movieList', 'regionTree','careerList']
    }
    http.request({
      url: url,
      data: data,
      methods: 'post',
      success: function (res) {
        //省份城市
        var region = res.data.regionTree;
        that.setData({
          region: region,
          jobarray: res.data.careerList,
          movieList: res.data.movieList
        });
        const provinces = [[], [], []];
        const citys = [[], []];
        const countys = [];
        //console.log("初始化省份");
        for (let i = 0; i < that.data.region.length; i++) {
          var obj = new Object;
          obj.itemid = that.data.region[i].region_id;
          obj.name = that.data.region[i].name
          provinces[0].push(obj);
        }

        that.setData({
          countysid: that.data.region[0].children[0].children[0].region_id,
          cityid: that.data.region[0].children[0].region_id,
          provinceid: that.data.region[0].region_id,
          province: that.data.region[0].name,
          city: that.data.region[0].children[0].name,
          countys: that.data.region[0].children[0].children[0].name
        })

        //console.log("初始化市区");
        for (let i = 0; i < that.data.region[0].children.length; i++) {
          var obj = new Object;
          obj.itemid = that.data.region[0].children[i].region_id;
          obj.name = that.data.region[0].children[i].name;
          provinces[1].push(obj);
        }

        //console.log("初始化区域");
        for (let i = 0; i < that.data.region[0].children[0].children.length; i++) {
          var obj = new Object;
          obj.itemid = that.data.region[0].children[0].children[0].region_id;
          obj.name = that.data.region[0].children[0].children[i].name;
          provinces[2].push(obj);
        }

        that.setData({
          'provinces': provinces,
          'citys': citys
        });
      }
    })

    //获取数据
      if(!!options.edit){
          http.request({
            url: 'mini/v1/index/demandinfo',
            data: { id: options.demand_id },
            success: function (res) {
              wx.setStorageSync('description', res.data.description)
              wx.createSelectorQuery().select('#editor').context(function (res) {
                that.editorCtx = res.context;
                that.editorCtx.setContents({
                  html: wx.getStorageSync('description')
                })
              }).exec()

              that.setData({
                title: res.data.name,
                job_id: res.data.career_id,
                job: res.data.career,
                start_data: res.data.start_date,
                end_data: res.data.end_date,
                city: res.data.region,
                provinceid: res.data.region_province_id,
                cityid: res.data.region_city_id,
                countysid: res.data.region_area_id,
                signing_type: res.data.signing_type,
                signing_prices: res.data.signing_price,
                movieid: res.data.movie_type_id,
                movie: res.data.movie_type
              })
            }
          });
      }
  },
  bindMultiPickerColumnChange: function (e) {
    var that = this;
    if (e.detail.column == 0) {
      that.setData({
        "provinces[1]": that.data.region[e.detail.value].children,
        "provinceid": that.data.region[e.detail.value].region_id,
        'key': e.detail.value,
        'province': that.data.region[e.detail.value].name,
        "provinces[2]": that.data.region[e.detail.value]['children'][0].children
      });
    } else if (e.detail.column == 1) {
      that.setData({
        "provinces[2]": that.data.region[that.data.key]['children'][e.detail.value].children,
        "cityid": that.data.region[that.data.key]['children'][e.detail.value].region_id,
        "city": that.data.region[that.data.key]['children'][e.detail.value].name
      });
    } else {
      that.setData({
        "countys": that.data.provinces[2][e.detail.value].name,
        "countysid": that.data.provinces[2][e.detail.value].region_id
      });
    }
  },
  bindMultiPickerChange: function (e) { //城市选择
    var that = this;
    var key = e.detail.value[0];
    var key0 = e.detail.value[1];
    var key1 = e.detail.value[2];

    if (key0 == null) {
      key0 = 0;
    }

    if (key1 == null) {
      key1 = 0;
    }

    that.setData({
      "countys": that.data.region["" + key + ""]['children']["" + key0 + ""]['children']["" + key1 + ""]['name'],
      "countysid": that.data.region["" + key + ""]['children']["" + key0 + ""]['children']["" + key1 + ""]['region_id'],
      "cityid": that.data.region["" + key + ""]['children']["" + key0 + ""]['region_id'],
      "city": that.data.region["" + key + ""]['children']["" + key0 + ""]['name'],
      "provinceid": that.data.region["" + key + ""]['region_id'],
      "province": that.data.region["" + key + ""]['name']
    });
  },
  //选择职业
  bindPickerChangejob(e) {
    this.setData({
      job: this.data.jobarray[e.detail.value].name,
      job_id: this.data.jobarray[e.detail.value].id
    })
  },
  //选择开始时间6
    bindPickerChangestart(e) {
      this.setData({
        start_data: e.detail.value
      })
    },
  //选择结束时间
  bindPickerChangend(e) {
    this.setData({
      end_data: e.detail.value
    })
  },
  bindPickerChangemovie(e){
    this.setData({
      movie: this.data.movieList[e.detail.value].name,
      movieid: this.data.movieList[e.detail.value].id
    })
  },
  //是否面议薪资
    checkboxChange(e) {
      if (e.detail.value[0]) {
        this.setData({
          signing_type: 1
        })
      } else {
        this.setData({
          signing_type: 0
        })
      }
    },
  //提交
   formSubmit: function (e) {
     console.log(this.data.provinceid)
     console.log(this.data.cityid)
     console.log(this.data.countysid)

     var that = this;
     var url = '';
     if (this.data.checkwhat == 'man') {
      if (that.data.checkurl == 'checkurl'){
        url = 'mini/v1/release/job'
      }else{
        url = 'mini/v1/release/job_patch'
      }
       
     } else {
       if (that.data.checkurl == 'checkurl') {
         url = 'mini/v1/release/lease'
       } else {
         url = 'mini/v1/release/lease_patch'
       }
       
     }
    var detail_infor = e.detail.value;
    //填写姓名
     if (!detail_infor.title) {
      wx.showToast({
        title: '请填写标题！',
        icon: 'none',
        duration: 2000
      });
      return;
    }

    
    //选择职业
     if (this.data.checkwhat == 'man') {
        if (!this.data.job_id) {
          wx.showToast({
            title: '请选择角色！',
            icon: 'none',
            duration: 2000
          });
          return;
        }
     }

     //选择职业
     if (this.data.checkwhat == 'man') {
       if (!this.data.movieid) {
         wx.showToast({
           title: '请选择影片类型！',
           icon: 'none',
           duration: 2000
         });
         return;
       }
     }

    //选择出生年月
     if (this.data.start_data == "开始时间") {
      wx.showToast({
        title: '请选择开始时间！',
        icon: 'none',
        duration: 2000
      });
      return;
    }

     if (this.data.end_data == "结束时间") {
       wx.showToast({
         title: '请选择结束时间！',
         icon: 'none',
         duration: 2000
       });
       return;
     }
   
    //薪资范围
    if (this.data.signing_type == 0) {
      if (!detail_infor.signing_price) {
        wx.showToast({
          title: '请填写薪资！',
          icon: 'none',
          duration: 2000
        });
        return;
      }
    }

    var signing_price = 0;
    if (this.data.signing_type == 0) {
      signing_price = detail_infor.signing_price
    } else {
      signing_price = 0
    }
    
        that.editorCtx.getContents({
        success: function (res) {
              //提交
              let data = {
                id: that.data.id,
                name: detail_infor.title,
                region_province_id: that.data.provinceid,
                region_city_id: that.data.cityid,
                region_area_id: that.data.countysid,
                signing_type: that.data.signing_type,
                start_date: that.data.start_data,
                end_date: that.data.end_data,
                signing_price: signing_price,
                description: res.html,
                career_id: that.data.job_id,
                movie_type_id: that.data.movieid
              };
              http.request({
                url: url,
                data: data,
                methods: 'post',
                success: function (res) {
                  app.globalData.checkup = that.data.checkwhat;
                  wx.switchTab({
                    url: '../../pages/index/index'
                  });
                }
              })
      }
    });
  },
  format(e) {
    let { name, value } = e.target.dataset
    if (!name) return
    this.editorCtx.format(name, value)
  },
  onEditorReady() {
    const that = this
    wx.createSelectorQuery().select('#editor').context(function (res) {
      that.editorCtx = res.context
    }).exec()
  },
  onShareAppMessage: function () {
    return util.share()
  }
})